package com.game;

import java.util.Vector;

import android.content.Context;
import android.graphics.Point;
import android.graphics.Canvas;
import android.graphics.PointF;

public class ColorBall extends Ball {

	private boolean exploded;
	private int speed;
	private int prevSpeed;
	Vector<MirrorBall> mMirrorBallsReference;
	private int ID;
	
	public boolean getExploded()
	{ return exploded;}
	
	public ColorBall(Context context, int imgRef, Point pos, BackgroundMirror bgmirror,Vector<MirrorBall> mbref, int speed, int id) 
	{
		super(context, imgRef, pos, bgmirror);
		mMirrorBallsReference = mbref;
		// Set some speed to the ball
		setSpeed(speed); 
		setPrevSpeed(0);
		
		exploded = false;
		ID = id;

	}
	
	public void Explode()
	{
		exploded = true;
	}
	
	public void Update()
	{
		super.Update();
		
		if(exploded) return;
		//Log.i("GAISMA LOG:: ", "-------------------------------- Starting ball update");
		CalculateCollision();
		UpdatePosition();
	}
	
	public void onDraw(Canvas canvas)
	{
		if(!exploded) super.onDraw(canvas);
	}
	
	private void CalculateCollision() 
	{
		// Calculate collision with mirror balls
		
		for(MirrorBall mirror : mMirrorBallsReference)
		{
			if(	Math.abs(getCenterX()- mirror.getCenterX()) < mirror.getRadius() &&
				Math.abs(getCenterY()- mirror.getCenterY()) < mirror.getRadius() &&
				mirror.getMirrorSet()) 				
			{
				// Is near
				if( !(mirror.ballCol.elementAt(ID).Coliding) )
				{					
					mirror.ballCol.elementAt(ID).Coliding = true;
					//The mirror is set
					PointF normalHeading, tangentHeading, normalToCollisionPlane;
					normalToCollisionPlane = new PointF();
					normalHeading = new PointF();
					tangentHeading = new PointF();
					
					normalToCollisionPlane.x = 0; 	normalToCollisionPlane.y = 0;  
					normalHeading.x = 0; 			normalHeading.y = 0;
					tangentHeading.x = 0; 			tangentHeading.y = 0;
					
					// Get the line connecting the laser tip and the mirror center
					normalToCollisionPlane.x = mirror.getCenterX() - getCenterX();
					normalToCollisionPlane.y = mirror.getCenterY() - getCenterY();
					
					// Get the normal component of the heading. This is done by projecting it
					// on the line that connects the center of the ball to the tip of the laser
					float proyectionLength =0;
					proyectionLength = 	heading.x*normalToCollisionPlane.x + 
										heading.y*normalToCollisionPlane.y;
					proyectionLength = proyectionLength / normalToCollisionPlane.length();
					
					// Normalize the normalToColPlane to get the unitary vector
					normalToCollisionPlane.x = normalToCollisionPlane.x/normalToCollisionPlane.length();
					normalToCollisionPlane.y = normalToCollisionPlane.y/normalToCollisionPlane.length();
					
					normalHeading.x = proyectionLength*normalToCollisionPlane.x;
					normalHeading.y = proyectionLength*normalToCollisionPlane.y; // Got it!!
					
					// Now we get the tangent, by simple vector decomposition: v = v.t+v.n
					tangentHeading.x = heading.x - normalHeading.x;
					tangentHeading.y = heading.y - normalHeading.y;
					
					// Finally, add to the tangent the negated normal to reflect away from the mirror
					heading.x =  (int) (tangentHeading.x  - normalHeading.x);
					heading.y =  (int) (tangentHeading.y  - normalHeading.y);
					
					// Don't forget, set the collision point to the surface of the sphere
					// Doesn't work baby
					//mPoints.elementAt(0).x = (int) (-normalToCollisionPlane.x* (mirror.getRadius()+1)) + mirror.getCenterX();
					//mPoints.elementAt(0).y = (int) (-normalToCollisionPlane.y* (mirror.getRadius()+1)) + mirror.getCenterY();
					
				}
			}
			else
			{mirror.ballCol.elementAt(ID).Coliding =false;}
		}
		
		// Calculate collision with walls
		if(getLeft() < mBackgroundMirror.getLeftLimit())
		{
			setLeft(mBackgroundMirror.getLeftLimit());
			heading.x = (-1)*heading.x;
			mBackgroundMirror.HitMirror();
		}
		else if (getRight() > mBackgroundMirror.getRightLimit())
		{		
			setRight(mBackgroundMirror.getRightLimit());
			heading.x = (-1)*heading.x;
			mBackgroundMirror.HitMirror();
		}
		
		if(getTop() < mBackgroundMirror.getTopLimit())
		{			
			setTop(mBackgroundMirror.getTopLimit());
			heading.y = (-1)*heading.y;
			mBackgroundMirror.HitMirror();
		}
		else if (getBottom() > mBackgroundMirror.getBottomLimit())
		{
			setBottom(mBackgroundMirror.getBottomLimit());
			heading.y = (-1)*heading.y;
			mBackgroundMirror.HitMirror();
		}		
		
	}
	
	private void UpdatePosition()
	{

		float length = (float)Math.sqrt(heading.x*heading.x + heading.y*heading.y);

		float normalizedx = (heading.x/length);
		float normalizedy = (heading.y/length);

		normalizedx *= speed;
		normalizedy *= speed;
		
		position.x += normalizedx;
		position.y += normalizedy;

	}
	
	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public int getSpeed() {
		return speed;
	}

	public void setPrevSpeed(int prevSpeed) {
		this.prevSpeed = prevSpeed;
	}

	public int getPrevSpeed() {
		return prevSpeed;
	}

	public void stopBall()
	{
		setPrevSpeed(speed);
		setSpeed(0);
	}
	
	public void resumeBall()
	{
		setSpeed(prevSpeed);
		setPrevSpeed(0);
	}
}
